﻿using System;
using System.Web;
using System.Xml.Linq;
using System.Linq;
namespace KMF.Utility
{
    public class Common
    {
        public static string GenerateId()
        {
            return MD52INT(Guid.NewGuid().ToString()).ToString();
        }

        public static int MD52INT(string MDKEY)
        {
            int TOTAL = 0;
            for (int i = 0; i < MDKEY.Length - 1; i++)
            {
                TOTAL += CHAR2INT(MDKEY.Substring(i, 1));
            }
            return TOTAL;
        }

        private static int CHAR2INT(string MYCHAR)
        {
            int NUM = 100;
            switch (MYCHAR)
            {
                case "A":
                    NUM = 19902;
                    break;
                case "B":
                    NUM = 15604;
                    break;
                case "C":
                    NUM = 17505;
                    break;
                case "D":
                    NUM = 15562;
                    break;
                case "E":
                    NUM = 18752;
                    break;
                case "F":
                    NUM = 1765712;
                    break;
                case "0":
                    NUM = 155675;
                    break;
                case "1":
                    NUM = 26767;
                    break;
                case "2":
                    NUM = 2567562;
                    break;
                case "3":
                    NUM = 15675692;
                    break;
                case "4":
                    NUM = 2567532;
                    break;
                case "5":
                    NUM = 1575682;
                    break;
                case "6":
                    NUM = 535392;
                    break;
                case "7":
                    NUM = 15354346;
                    break;
                case "8":
                    NUM = 1723427;
                    break;
                case "9":
                    NUM = 1342399;
                    break;
            }
            return NUM;
        }

        public static string returnConfig(string nodeName)
        {
            var pathfile = "~/Areas/KMFadmin/KMFsystem.xml"; //ConfigurationManager.AppSettings["HostContainImages"] 
            var xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath(pathfile));
            var elementValue = (from item in xmldoc.Descendants("KMFsystem")
                                select (string)item.Element(nodeName)).SingleOrDefault();
            return elementValue;
        }
        public static string returnConfigByAttribute(string nodeName,string attr)
        {
            var pathfile = "~/Areas/KMFadmin/KMFsystem.xml";
            var xmldoc = XDocument.Load(HttpContext.Current.Server.MapPath(pathfile));
            var elementValue = (from item in xmldoc.Descendants("KMFsystem")
                                select (string)item.Element(nodeName).Attribute(attr)).SingleOrDefault();
            return elementValue;
        }
    }
}
